package com.weishi.project.training.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.weishi.common.utils.StringUtils;
import com.weishi.common.utils.poi.ExcelUtil;
import com.weishi.framework.aspectj.lang.annotation.Log;
import com.weishi.framework.aspectj.lang.enums.BusinessType;
import com.weishi.framework.web.controller.BaseController;
import com.weishi.framework.web.domain.AjaxResult;
import com.weishi.framework.web.page.TableDataInfo;
import com.weishi.project.training.domain.Sportsman;
import com.weishi.project.training.domain.SportsmanPhysicalQuality;
import com.weishi.project.training.service.ISportsmanPhysicalQualityService;
import com.weishi.project.training.service.ISportsmanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Api(value = "PC端 运动员物理属性接口",description = "PC端 运动员物理属性接口")
@RestController
@RequestMapping("/training/sportsmanPhysicalQuality")
public class SportsmanPhysicalQualityController extends BaseController {

    @Autowired
    private ISportsmanPhysicalQualityService sportsmanPhysicalQualityService;

    @Autowired
    private ISportsmanService sportsmanService;


    /**
     * 查询运动员物理属性列表
     */
    @PreAuthorize("@ss.hasPermi('training:sportsmanPhysicalQuality:list')")
    @GetMapping("/list")
    public TableDataInfo list(SportsmanPhysicalQuality sportsmanPhysicalQuality) {
        startPage();
        List<SportsmanPhysicalQuality> list = sportsmanPhysicalQualityService.selectSportsmanPhysicalQualityList(sportsmanPhysicalQuality);
        return getDataTable(list);
    }

    @ApiOperation("导出运动员物理属性export 列表")
    @PreAuthorize("@ss.hasPermi('training:sportsmanPhysicalQuality:export')")
    @Log(title = "sportsmanPhysicalQuality", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SportsmanPhysicalQuality sportsmanPhysicalQuality) {
        List<SportsmanPhysicalQuality> list = sportsmanPhysicalQualityService.selectSportsmanPhysicalQualityList(sportsmanPhysicalQuality);
        ExcelUtil<SportsmanPhysicalQuality> util = new ExcelUtil<SportsmanPhysicalQuality>(SportsmanPhysicalQuality.class);
        return util.exportExcel(list, "运动员物理属性");
    }

    @ApiOperation("导入列表数据")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<SportsmanPhysicalQuality> util = new ExcelUtil<>(SportsmanPhysicalQuality.class);
        List<SportsmanPhysicalQuality> userList = util.importExcel(file.getInputStream());
        userList.forEach( p-> {
            if(StringUtils.isNotEmpty(p.getSportsmanName())){
                Sportsman sportsman = sportsmanService.qrySportsmanByName(p.getSportsmanName());
                if(sportsman!=null) {
                    p.setSportsmanId(sportsman.getId());
                    p.setSportsmanName(sportsman.getName());
                    sportsmanPhysicalQualityService.save(p);
                }
            }
        });
        return AjaxResult.success();
    }

    @ApiOperation("导入列表模板")
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        ExcelUtil<SportsmanPhysicalQuality> util = new ExcelUtil<>(SportsmanPhysicalQuality.class);
        return util.importTemplateExcel("模板数据");
    }

    /**
     * 获取运动员物理属性详细信息
     */
    @PreAuthorize("@ss.hasPermi('training:sportsmanPhysicalQuality:query')")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(sportsmanPhysicalQualityService.getById(id));
    }

    /**
     * 新增运动员物理属性
     */
    @RequestMapping(value = "/add", method = {RequestMethod.POST})
    @ResponseBody
    public AjaxResult add(@RequestBody SportsmanPhysicalQuality sportsmanPhysicalQuality) {
        sportsmanPhysicalQuality.setCtime(new Date());
        List<Sportsman> sportsmanList = sportsmanService
                .list(Wrappers.<Sportsman>lambdaQuery()
                        .eq(Sportsman::getId , sportsmanPhysicalQuality.getSportsmanId()));

        if(sportsmanList.size() > 0){
            sportsmanPhysicalQuality.setSportsmanName(sportsmanList.get(0).getName());
        }
        sportsmanPhysicalQualityService.save(sportsmanPhysicalQuality);
        return AjaxResult.success();
    }

    /**
     * 修改运动员物理属性
     */
    @RequestMapping(value = "/update", method = {RequestMethod.POST})
    @ResponseBody
    public AjaxResult edit(@RequestBody SportsmanPhysicalQuality sportsmanPhysicalQuality) {
        List<Sportsman> sportsmanList = sportsmanService
                .list(Wrappers.<Sportsman>lambdaQuery()
                        .eq(Sportsman::getId , sportsmanPhysicalQuality.getSportsmanId()));

        if(sportsmanList.size() > 0){
            sportsmanPhysicalQuality.setSportsmanName(sportsmanList.get(0).getName());
        }
        sportsmanPhysicalQualityService.updateById(sportsmanPhysicalQuality);
        return AjaxResult.success();
    }

    /**
     * 删除运动员物理属性
     */
    @PreAuthorize("@ss.hasPermi('training:sportsmanPhysicalQuality:remove')")
    @Log(title = "sportsmanPhysicalQuality", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        sportsmanPhysicalQualityService.removeByIds(Arrays.asList(ids));
        return AjaxResult.success();
    }

    @PreAuthorize("@ss.hasPermi('training:sportsmanPhysicalQuality:list')")
    @GetMapping("/queryNameList")
    public TableDataInfo querySportsNameList() {
        startPage();
        List<Sportsman> list = sportsmanPhysicalQualityService.querySportsNameList();
        return getDataTable(list);
    }
    
}